<?php

namespace app\index\controller;

use app\index\model\File as ModelFile;
use frame\lib\Db;
use frame\lib\VerfiyParam;

class File extends Base
{
    public function index()
    {
        $this->view('index');
    }
    public function list()
    {
        $fileName = @$_GET['file'];
        if (!empty($fake)) {
            $fileName = trim($fileName);
        }
        $list = ModelFile::list($fileName);
        success($list);
    }
    public function del()
    {
        $id = @$_GET['id'];
        if (!$id) error('缺少参数ID', 201);
        $id = explode(',', $id);
        $res = Db::name('file')->where(['uid' => $this->uid])->whereIn('id', $id)->delete();
        if ($res) success();
        error();
    }
    public function doEdit()
    {
        $param = $_POST;
        $rule = [
            'id' => 'require|int',
            'content' => 'require'
        ];
        VerfiyParam::init($param, $rule);
        $param['update_time'] = getTime();
        $res = Db::name('file')->where(['uid' => $this->uid, 'id' => $param['id']])->update($param);
        if ($res) success();
        error();
    }
    public function add()
    {
        $this->view('add');
    }
    public function doAdd()
    {
        $param = $_POST;
        $fileName = randStr(10) . date('Yhd', time());
        $rule = ['content|内容' => 'require'];
        VerfiyParam::init($param, $rule);
        $limit = Db::name('user_limit')->where(['uid' => $this->uid])->find();
        $fileNum = Db::name('file')->where(['uid' => $this->uid])->count();
        if ($limit['file'] < $fileNum + 1) error('文档库存已满，如需添加，请联系管理员');
        $data = [
            'uid' => $this->uid,
            'file_name' => $fileName,
            'content' => $param['content'],
            'create_time' => getTime()
        ];
        $res = Db::name('file')->insert($data);
        if ($res) success();
        error();
    }
}
